package com.yjymm.commonsdk.config.interceptor;

import com.yjymm.commonsdk.common.LoginUserContext;
import com.yjymm.commonsdk.model.RequestLogMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author panhaoxiong
 * @date 2021-02-07 16:52
 */
@Slf4j
public class RequestLogInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        String referer = request.getHeader("Referer");
        String ip = request.getRemoteAddr();
        log.info("--------- 请求uri:{} ---------",requestURI);
        log.info("--------- 请求referer:{} ---------",referer);
        log.info("--------- 请求ip:{} ---------",ip);
        RequestLogMessage build = RequestLogMessage.builder()
                .requestURI(requestURI)
                .referer(referer)
                .ip(ip)
                .build();
        LoginUserContext.requestLogMessage.set(build);
        return true;

    }
}
